package com.aa;

import org.json.JSONException;
import org.json.JSONObject;

import com.aa.http.HttpOperation;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class BalanceActivity extends Activity {
	  private String TAG = "BalanceActivity";
	  private String mUrl = "http://121.9.64.196:8087/AAproject/api/mobile.php";
	  TextView mTitleView;
	  ProgressDialog progressDialog = null;
	  TextView textView1;
	  TextView textView2;
	  TextView textView3;
	  TextView textView4;
	  TextView textView5;
	  TextView textView6;
	  private Button title_btn_back;
	  private Button title_btn_ok;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.balance);
	
		    this.mTitleView = ((TextView)findViewById(R.id.app_title_center));
		    this.mTitleView.setText(R.string.balance_title);
		    this.textView1 = ((TextView)findViewById(R.id.balance_textVew1));
		    this.textView2 = ((TextView)findViewById(R.id.balance_textVew2));
		    this.textView3 = ((TextView)findViewById(R.id.balance_textVew3));
		    this.textView4 = ((TextView)findViewById(R.id.balance_textVew4));
		    this.textView5 = ((TextView)findViewById(R.id.balance_textVew5));
		    this.textView6 = ((TextView)findViewById(R.id.balance_textVew6));
		    this.textView1.setText(Common.iBalance);
		    this.textView2.setText(Common.iMyPhoneNumber);
		    this.textView3.setText("");
		    this.textView4.setText("");
		    this.textView5.setText("");
		    this.textView6.setText("");
		    this.title_btn_back = ((Button)findViewById(R.id.title_btn1));
		    this.title_btn_back.setVisibility(1);
		    this.title_btn_back.setText(R.string.app_back_tip);
		    this.title_btn_back.setOnClickListener(new View.OnClickListener()
		    {
		      public void onClick(View paramAnonymousView)
		      {
		        BalanceActivity.this.finish();
		      }
		    });
		    this.title_btn_ok = ((Button)findViewById(R.id.title_btn4));
		    this.title_btn_ok.setVisibility(1);
		    this.title_btn_ok.setText(R.string.app_refurbish_tip);
		    this.title_btn_ok.setOnClickListener(new View.OnClickListener()
		    {
		      public void onClick(View paramAnonymousView)
		      {
		        BalanceActivity.this.submitQuery();
		      }
		    });
		    submitQuery();
	}

	private void submitQuery(){

		final JSONObject _SendJson = new JSONObject();
		JSONObject _BodyJson = new JSONObject();
		JSONObject _HeadJson = new JSONObject();
		try {
			_HeadJson.put("v", Common.iVersion);
			_HeadJson.put("time", System.currentTimeMillis());
			_HeadJson.put("cmd", 5);
			_HeadJson.put("mac", Common.iMac);
			
			_BodyJson.put("uid", Common.iUID);
			_BodyJson.put("pnumber", Common.iMyPhoneNumber);
			
			_SendJson.put("head", _HeadJson);
			_SendJson.put("body", _BodyJson);
			Log.i(TAG, "send = " + _SendJson.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		new Thread(new Runnable() {				
			@Override
			public void run() {
			
				String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
				
				if (_Result == null || _Result.equals("ConnectTimeout")) {
					return;
				}
				Log.i(TAG, " return _Result = " + _Result.toString());	
				try {
					  JSONObject _JsonObject = new JSONObject(_Result);
						  Log.i(TAG, "_Result = " + _Result);	
						
					  JSONObject _Head = _JsonObject.getJSONObject("head");  
					  JSONObject _Body = _JsonObject.getJSONObject("body");
					  if (_Body.getString("rcode").equals("1")) {
						
						 // mHandler.sendEmptyMessage(0);
						 
					}else {
						//mHandler.sendEmptyMessage(1);
					}
					
				} catch (JSONException e) {
					 
					e.printStackTrace();
				}					
			}
		}).start();		
	}
}
